# -*- coding: utf-8 -*-
__author__ = 'xiaobo'
from transwarp import db

# db.init(db_type='mysql' ,db_host='127.0.0.1',db_user='root', db_password='', db_schema='test',  port=3306)
# db.create_engine(user='root', password='c8@ki4dANJEHS$', database='cafuc_prod', host='127.0.0.1', port=3306)
# db.create_engine(user='root', password='123456', database='cafuc', host='127.0.0.1', port=3306)
# db.create_engine(user='root', password='123456', database='ledong', host='127.0.0.1', port=3306)
db.create_engine(user='root', password='123456', database='test', host='127.0.0.1', port=3306)


# n = db.update('insert into user (id, name,rec_time) values (?,?,?)',2, u'张三',changeStr2Time('2015年06月14日'))
# users = db.select('select * from user')
# for user in users:
#     print user['name']

# users = db.select('select * from history_mp_record where id = ?',1)
# for user in users:
#     print user['MP_URL']

def saveMpRecord(name,id_card,college_name,group_name,group_id,mp_url,fly_time):
    db.update('INSERT INTO `history_mp_record` ( `STUDENT_NAME`, `ID_CARD`, `COLLEGE_NAME`, `GROUP_NAME`, `GROUP_ID`, '
              '`MP_URL`, `FLY_TIME`,`COMPLETE`) '
              'VALUES (?,?,?,?,?, ?,?,?)',
                  name, id_card, college_name,group_name, group_id, mp_url, fly_time,0)

def saveMpRecordUpdate(name,id_card,college_name,group_name,group_id,mp_url,fly_time):
    db.update('INSERT INTO `history_mp_record_update` ( `STUDENT_NAME`, `ID_CARD`, `COLLEGE_NAME`, `GROUP_NAME`, `GROUP_ID`, '
              '`MP_URL`, `FLY_TIME`,`COMPLETE`) '
              'VALUES (?,?,?,?,?, ?,?,?)',
                  name, id_card, college_name,group_name, group_id, mp_url, fly_time,0)
def truncateMpRecordUpdate():
    db.update('truncate table `history_mp_record_update`')

def updateMpRecordComp(id):
     n = db.update('update `history_mp_record` set COMPLETE = 1 where id = ?',id)

# updateMpRecordComp(1)
def saveTpRecord(MP_ID, TOPIC_NAME, TOPIC_URL):
    db.update('INSERT INTO `history_topic_record` ( `MP_ID`, `TOPIC_NAME`, `TOPIC_URL`, `COMPLETE`)'
              ' VALUES ( ?,?,?,?)',MP_ID,TOPIC_NAME,TOPIC_URL,0)

def updateTpRecordComplete(id):
    db.update('update `history_topic_record` set COMPLETE = 1 where id = ?',id)

def getAllMpId():
    return  db.select('select id,mp_url from history_mp_record where COMPLETE = 0')

def getAllTpId():
    return  db.select('SELECT tp.ID,tp.TOPIC_URL,tp.TOPIC_NAME, mp.GROUP_ID ,mp.ID_CARD from history_topic_record tp'
     ' left JOIN history_mp_record mp on tp.MP_ID = mp.ID'
    ' where tp.COMPLETE = 0')
    # return  db.select('select id from history_mp_record where id<2')

def getProvinceLastId():
    return  db.select('select max(id) as id from code_province')
def getCityLastId():
    return  db.select('select max(id) as id from code_city')
def getDistrictLastId():
    return  db.select('select max(id) as id from code_district')

def updateProvinceCode(code,id):
    db.update('update `code_province` set code = ? where id = ? ',code,id)
def updateCityCode(code,id):
    db.update('update `code_city` set code = ? where id = ? ',code,id)
def updateDistrictCode(code,id):
    db.update('update `code_district` set code = ? where id = ? ',code,id)

def saveProvince(name):
    db.update('INSERT INTO `code_province` ( `code_name`)'
              ' VALUES ( ?)',name)
def getProvinceId(name):
    return  db.select('select id from `code_province` where code_name = ?',name)
def saveCity(name,pId):
    db.update('INSERT INTO `code_city` (`code_name`, `province_id`)'
              ' VALUES ( ?, ?)',name,pId)
def getCityId(name):
    return  db.select('select id from `code_city` where code_name = ?',name)
def saveArea(name,cId):
    db.update('INSERT INTO `code_district` (`code_name`, `city_id`) '
              'VALUES (?, ?)',name,cId)

def saveCountry(code,name):
    db.update('INSERT INTO `code_country_region` (`code`,`code_name`) '
              'VALUES (?,?)',code,name)
def saveEducational(code,name):
    db.update('INSERT INTO `code_educational` (`code`,`code_name`) '
              'VALUES (?,?)',code,name)
def saveProfession(code,name):
    db.update('INSERT INTO `code_profession` (`code`,`code_name`) '
              'VALUES (?,?)',code,name)
def saveSchoolRoleLevel(code,name):
    db.update('INSERT INTO `code_school_role_level` (`code`,`code_name`) '
              'VALUES (?,?)',code,name)

def saveStudentType(code,name):
    db.update('INSERT INTO `code_student_type` (`code`,`code_name`) '
              'VALUES (?,?)',code,name)

if __name__ == '__main__':
    # pName = 'pp'
    # cName = 'cc'
    # aName = 'aa'
    # saveProvince(pName)
    # # print getProvinceId(pName)
    # saveCity(cName,getProvinceId(pName)[0].id)
    # saveArea(aName,getCityId(cName)[0].id)
    pName = u"请选择"
    if pName==u"请选择":
        print 'yes'